package com.lsw.jgyf.ui.fragment;


import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.util.LogUtils;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by lsw on 2017/12/7.
 */

public class MyOrderFragmentPresenter extends BasePresenter<MyOrderFragmentContract.View> implements MyOrderFragmentContract.Presenter {
    @Inject
    HttpApi httpApi;

    @Inject
    public MyOrderFragmentPresenter() {
    }

    @Override
    public void getData(String search, String status, String page) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .getOrder(search, status, page)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    mView.RefreshSucceed();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        if (baseBean.getData().getData().size() < Constants.PAGE_DEFAULT_COUNT) {
                            mView.setData(baseBean.getData(), false);
                        } else
                            mView.setData(baseBean.getData(), true);
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    mView.RefreshFail();
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void AuditSuccess(String id) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .AuditSuccess(id)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                        mView.AuditSuccess(baseBean.getData());
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void AuditFailure(String id, String deny_reason) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .AuditFailure(id, deny_reason)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                        mView.AuditFailure(baseBean.getData());
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }
}
